<style lang="scss" scoped>
.mask-content {
    max-height: 90%;
    width: 460px;
    overflow-y: auto;
    overflow-x: hidden;
    background-color: #fff;
    border-radius: 6px;
    // box-sizing: border-box;
    padding: 40px 30px 10px;
}

input {
    border: none;
    outline: none;
    border-bottom: 1px solid #9e9e9e;
    padding: 5px 0;
    width: 100%;

    font-size: 16px;

    &::placeholder {
        color: #9f9f9f;
    }
}

.form-item {
    margin-bottom: 40px;

    &:last-child {
        margin-bottom: 15px;
    }
}

.form-item-name {
    color: #696969;
    font-size: 13px;
    margin-bottom: 2px;
}

button {
    border: none;
    cursor: pointer;
    background-color: #f44336;
    color: #fff;
    width: 100%;
    font-size: 16px;
    padding: 8px;
    border-radius: 4px;
    box-shadow: 0px 1px 2px 1px rgb(0 0 0 / 23%);
}

.entrance-tip {
    font-size: 15px;
    align-items: center;
    font-size: 14px;
    color: #333333;

    span {
        margin-right: 5px;
    }
}

.entrance-social {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 20px;

    &::before {
        content: '';
        display: block;
        position: absolute;
        height: 1px;
        background-color: #d8d8d8;
        width: 60%;
    }

    span {
        position: absolute;
        font-size: 13px;
        color: #b5b5b5;
        z-index: 10;
        background-color: #fff;
        padding: 0 10px;
    }
}

.social-login {
    display: flex;
    justify-content: center;
}

span.iconfont {
    cursor: pointer;
    margin: 0 8px;
    width: 28px;
    height: 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    color: #fff;
    font-size: 18px;

    &.icon-weibo {
        background-color: #e05244;
    }

    &.icon-qq {
        background-color: #00aaee;
    }
}

.check-code {
    display: flex;

    a {
        word-break: keep-all;
        padding-bottom: 0;
        color: #000;
        padding:10px 12px 0;
        font-size: 13px;
    }
}

a.not-click {
    cursor: not-allowed;
    color: #b5b5b5;
}
</style>

<template>
    <div class="mask-content">
        <div class="form-item">
            <div class="form-item-name">邮箱号</div>
            <input v-model="registerForm.email" placeholder="请输入您的邮箱" />
        </div>
        <div class="form-item">
            <div class="form-item-name">验证码</div>
            <div class="check-code">
                <input v-model="registerForm.emailCode" placeholder="请输入验证码" />
                <a href="#" :disabled="emailSended" :class="emailSended?'not-click':''">
                    <span>
                        <span v-if="!emailSended" @click="sendEmailCode">
                            发送
                        </span>
                        <span v-else>
                            <em style="font-style: unset;">{{ elapsedTime }}s</em>后可重新发送
                        </span>
                    </span>
                </a>

            </div>
        </div>
        <div class="form-item">
            <div class="form-item-name">密码</div>
            <input type="password" v-model="registerForm.password" placeholder="请输入您的密码" />
        </div>
        <div class="form-item">
            <div class="form-item-name">确认密码</div>
            <input type="password" v-model="registerForm.checkPassword" placeholder="请再次输入您的密码" />
        </div>
        <div class="form-item">
            <button @click="doRegister">注册</button>
        </div>
        <div class="form-item">
            <div class="entrance-tip">
                <span>已有账号?</span>
                <a href="#" @click="changeComponent('LoginBox')">登录</a>
            </div>
        </div>
    </div>
</template>

<script>
import { getEmailCode as getEmailCodeApi, registerMail} from '@/api/loginApi'
export default {
    name: 'RegisterBox',
    data() {
        return {
            registerForm: {
                email: '',
                emailCode: '',
                password: '',
                checkPassword: '',
            },
            elapsedTime: 60,
            emailSended: false
        }
    },
    methods: {
        changeComponent(name) {
            this.$store.commit('modal/updateComponentName', name)
        },
        sendEmailCode() {

            if(!this.registerForm.email) {
                this.$toast('warn','请输入邮箱')
                return
            }
            if(!/\w+?@\w+?\.\w+?/.test(this.registerForm.email)) {
                this.$toast('warn','邮箱格式不正确')
                return
            }

            this.emailSended = true

            getEmailCodeApi({ email: this.registerForm.email, type: 1 }).then(res => {
                let intervalId = setInterval(() => {
                    if (this.elapsedTime == 0) {
                        this.emailSended = false
                        this.elapsedTime = 60
                        window.clearInterval(intervalId)
                    } else {
                        this.elapsedTime--
                    }
                }, 1000)
                this.$toast('success', '发送成功')
            }).catch(err=>{
                this.emailSended = false
            })
        },
        doRegister() {
            registerMail(this.registerForm).then(flag=>{
                if(flag) {
                    this.$toast('success', '恭喜您注册成功')
                    this.changeComponent('LoginBox')
                }
            })
        }
    },
    components: {
    }
}
</script>

